package xjtu.sad.zbsj.common;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class CommonAPI {
	@Resource
	private CommonService commonService;

	@GetMapping("/avatar/byname")
	public Result getU_avatarByU_name(@RequestParam("u_name") String u_name) {
		String u_avatar = commonService.getU_avatarByU_name(u_name);

		if (u_avatar==null || Constants.IMG_PATH_HOST.equals(u_avatar) || (Constants.IMG_PATH_HOST+"null").equals(u_avatar) ) {
			u_avatar = Constants.IMG_PATH_HOST + Constants.DEFAULT_AVATAR;
		}
		return Result.successData(u_avatar);
	}

    @GetMapping("/select/u_name")
    public Result selectU_nameByU_id(@RequestParam("u_id") String u_id) {
    	try {
			String u_name = commonService.selectU_nameByU_id(u_id);

			if (u_name==null) {
				return Result.fail(Result.ERR_CODE_BUSINESS, "无此用户！");
			}

			return Result.successData(u_name);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(Result.ERR_CODE_SYS, "查询失败！");
		}
    }

    @GetMapping("/select/u_name/bytoken")
    public Result selectU_nameByToken(@RequestHeader("Token") String token) {
    	try {
			String u_name = TokenUtils.getUserInfo(token, commonService).getU_name();
			return Result.successData(u_name);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(Result.ERR_CODE_BUSINESS, "Token错误！");
		}
    }


	/**
	 * 上传图片到服务器指定文件夹下
	 * @param multipartFile 上传的图片文件
	 * @param token 令牌
	 * @return 前端服务器能够直接使用的图片地址，如 http://106.15.91.23/img/defaultAvatar.png
	 */
	@PostMapping("/upload/img")
	public Result uploadImg(@RequestParam("file") MultipartFile multipartFile, @RequestHeader("Token") String token) {
		try {
			String u_id = TokenUtils.getUserInfo(token, commonService).getU_id();
//			String u_id = "U202111060001";
			String imgUrl = commonService.uploadImg(multipartFile, u_id);
			return Result.success("上传成功！", imgUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(Result.ERR_CODE_SYS, "系统错U202111060001误！图片上传失败！");
		}
	}

	/**
	 * CKEditor 上传图片
	 */
	@PostMapping("/upload/img/ckeditor")
	public Map<String, Object> uploadImgCKEditor(@RequestParam("upload") MultipartFile multipartFile, @RequestHeader("Token") String token) {

		Map<String , Object> result = new HashMap<>();
		Map<String , String> errMsg = new HashMap<>();
		/*
		 * 检查文件是否符合上传要求
		 * 1. 检查当前空间是否足够;
		 * 2. 检查文件是否超出上传大小限制;
		 */
		Long fileSize = multipartFile.getSize();
		String u_id = TokenUtils.getUserInfo(token, commonService).getU_id();
		Long maxUploadSize = 1000000000000L;

		if (fileSize > maxUploadSize) {
			errMsg.put("message", "文件过大！无法上传！请上传小于"+maxUploadSize/1024/1024+"MB的文件！");
			result.put("error", errMsg);
			return result;
		}

		Result uploadResult = uploadImg(multipartFile, token);
		if (!uploadResult.getSuccess()) {
			errMsg.put("message", uploadResult.getMessage());
			result.put("error", errMsg);
		} else {
			result.put("url", uploadResult.getData());
		}
		return result;
	}

}
